home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Taifun / Taifun 099 (1989-05-15)(Ossowski, Stefan)(DE)(PD).zip / Taifun 099 (1989-05-15)(Ossowski, Stefan)(DE)(PD).adf / PCQ / Runtime / readint.asm < prev    next >
Assembly Source File  |  1989-03-31  |  3KB  |  114 lines

  1.  
  2. *    ReadInt.asm (of PCQ Pascal runtime library)
  3. *    Copyright (c) 1989 Patrick Quaid
  4.  
  5. *    This routine reads an integer from a text file.  Note that
  6. *    normal files and standard input are handled very differently.  This
  7. *    is because we always have the next character from a normal file in
  8. *    its buffer, but we would not require the same from standard in
  9. *    because it would make the user type something on the terminal.
  10.  
  11.     SECTION    ONE
  12.  
  13.     XREF    _p%readonechar
  14.     XREF    _p%getthatchar
  15.     XREF    stdinbuffed
  16.     XREF    _p%readarbbuf
  17.  
  18.     XDEF    _p%readint
  19. _p%readint
  20.  
  21. * At the outset, a0 has the address of the variable and the address
  22. * of the file record is on the stack
  23.  
  24.     move.l    a0,-(sp)    ; save the variable address
  25.     move.l    8(sp),a0    ; get the file record
  26.     move.l    a0,d0
  27.     bne    readintfile    ; if it is a file, go around
  28.  
  29.     moveq    #0,d1        ; clear the number in case of eof
  30. 1$    jsr    _p%readonechar    ; get the next character
  31.     jsr    _p%getthatchar    ; get infile^
  32.     cmp.b    #' ',d0        ; is it a space?
  33.     beq    1$        ; if so, read on
  34.     cmp.b    #9,d0        ; is it a tab?
  35.     beq    1$        ; read on
  36.  
  37.     moveq.l    #0,d1        ; starts at zero
  38.     moveq.w    #1,d2        ; and positive
  39.     cmp.b    #'-',d0        ; is it the minus sign?
  40.     bne.s    3$        ; if not, go around.
  41.     moveq.w    #-1,d2        ; negative numbers
  42.     bra.s    6$        ; eat the minus sign
  43.  
  44. 3$    cmp.b    #'0',d0        ; is it numeric?
  45.     bge.s    7$        ; if > 0 then continue
  46.     move.b    #-1,stdinbuffed    ; if not, re-buffer char
  47.     bra.s    4$        ; and leave
  48. 7$    cmp.b    #'9',d0        ; numeric?
  49.     ble.s    8$        ; if so, go on
  50.     move.b    #-1,stdinbuffed    ; if not, re-buffer char
  51.     bra.s    4$        ; and leave
  52. 8$    asl.l    #1,d1        ; d1 = d1 * 2
  53.     move.l    d1,-(sp)    ; store d1 * 2
  54.     asl.l    #2,d1        ; d1 = d1 * 4 (* 8 total)
  55.     add.l    (sp)+,d1    ; finally, d1= d1*8 + d1*2= d1*10
  56.     sub.b    #'0',d0        ; get the actual number
  57.     and.l    #255,d0        ; always positive, so now it's long
  58.     add.l    d0,d1        ; d1= d1*10 + d0
  59. 6$    jsr    _p%readonechar    ; read next char
  60.     jsr    _p%getthatchar    ; move new char into d0
  61.     bra    3$        ; and start again
  62. 4$    move.l    (sp)+,a0    ; get address of variable
  63.     tst.w    d2        ; is this negative?
  64.     bpl.s    5$        ; if not, skip
  65.     neg.l    d1        ; if so, negate the result
  66. 5$    move.l    d1,d0        ; store final number
  67.     rts
  68.  
  69. readintfile:            ; read an int from a file
  70.     moveq    #0,d1        ; clear the number in case of eof
  71.     bra.s    2$        ; skip the first read
  72. 1$    tst.b    12(a0)        ; at eof yet?
  73.     bne    4$        ; if so, leave
  74.     jsr    _p%readarbbuf    ; get the next character
  75. 2$    move.b    4(a0),d0    ; get infile^
  76.     cmp.b    #' ',d0        ; is it a space?
  77.     beq    1$        ; if so, read on
  78.     cmp.b    #9,d0        ; is it a tab?
  79.     beq    1$        ; read on
  80.  
  81.     moveq.l    #0,d1        ; starts at zero
  82.     moveq.w    #1,d2        ; and positive
  83.     cmp.b    #'-',d0        ; is it the minus sign?
  84.     bne.s    3$        ; if not, go around.
  85.     moveq.w    #-1,d2        ; negative numbers
  86.     bra.s    6$        ; eat the minus sign
  87.  
  88. 3$    cmp.b    #'0',d0        ; is it numeric?
  89.     blt.s    4$        ; if not, go away
  90.     cmp.b    #'9',d0        ; numeric?
  91.     bgt.s    4$        ; if not, leave
  92.     asl.l    #1,d1        ; d1 = d1 * 2
  93.     move.l    d1,-(sp)    ; store d1 * 2
  94.     asl.l    #2,d1        ; d1 = d1 * 4 (* 8 total)
  95.     add.l    (sp)+,d1    ; finally, d1= d1*8 + d1*2= d1*10
  96.     sub.b    #'0',d0        ; get the actual number
  97.     and.l    #255,d0        ; always positive, so now it's long
  98.     add.l    d0,d1        ; d1= d1*10 + d0
  99. 6$    tst.b    12(a0)        ; at eof yet?
  100.     bne.s    4$        ; if so, leave
  101.     movem.l    d1/d2,-(sp)    ; save number and sign for now
  102.     jsr    _p%readarbbuf    ; read next char
  103.     movem.l    (sp)+,d1/d2    ; retrieve them
  104.     move.b    4(a0),d0    ; move new char into d0
  105.     bra    3$        ; and start again
  106. 4$    move.l    (sp)+,a0    ; get address of variable
  107.     tst.w    d2        ; is this negative?
  108.     bpl.s    5$        ; if not, skip
  109.     neg.l    d1        ; if so, negate the result
  110. 5$    move.l    d1,d0        ; store final number
  111.     rts
  112.  
  113.     END
  114.